package com.fjec.base.shiro;

import com.fjec.common.enums.EnumLoginType;
import com.fjec.common.shiro.MyUserToken;
import com.google.common.base.Strings;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

public class WechatAppRealm extends AuthorizingRealm {

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        MyUserToken loginToken = (MyUserToken) token;
        String openId = loginToken.getUsername();
        String pwd = new String(loginToken.getPassword());
        if (Strings.isNullOrEmpty(openId)) {
            return null;
        } else {
            return new SimpleAuthenticationInfo(openId, pwd, getName());
        }
    }

    @Override
    public boolean supports(AuthenticationToken token) {
        MyUserToken loginToken = (MyUserToken) token;
        if (loginToken.getLoginType() == EnumLoginType.WECHAT_APP) {
            return true;
        } else {
            return false;
        }
    }
}
